package com.zhoukx.ocp;

/**
 * @ClassName ocp
 * @Description: 开闭原则
 * @Author zhoukx
 * @Date 2020/8/4
 **/
public class Ocp {
    public static void main(String[] args) {
        GraphicEditor graphicEditor = new GraphicEditor();
        graphicEditor.drawShape(new Rectangle());
        graphicEditor.drawShape(new Circle());
        graphicEditor.drawShape(new Triangle());
    }
}


//  用于绘图的类
class GraphicEditor {
    // 接收shape对象，然后根据type，绘制不同的图
    public void drawShape(Shape s) {
        if (s.m_type == 1)
            drawRectangle(s);
        else if (s.m_type == 2)
            drawCircle(s);
        else  if(s.m_type == 3)
            drawTriangle(s);
    }

    public void drawRectangle(Shape r) {
        System.out.println("矩形");
    }

    public void drawCircle(Shape r) {
        System.out.println("圆形");
    }

    public void drawTriangle(Shape r) {
        System.out.println("三角形");
    }
}
// Shape 基类
class Shape {
    int m_type;
}
// 绘制矩形
class Rectangle extends Shape {
    Rectangle() {
        super.m_type = 1;
    }
}
// 绘制圆形
class Circle extends Shape {
    Circle() {
        super.m_type = 2;
    }
}
// 新增画三角形
class Triangle extends  Shape{
    Triangle(){
        super.m_type = 3;
    }
}
